<?php
/* explain:		news system
 * Project:     
 * File:        news.php
 *
 * @ link 		http://www.../modules/
 * @ Email		ldmmyx@hotmail.com
 * @ copyright 	2005  Ling Deming
 * @ author 	ivan.ling  Ling Deming
 * @ version 	1.0
 *
 */
include_once("Common.php");
include_once("Logs.php");
class PromotionImpl extends Common
{
	public $intLinkSign;
	public $errorInfo;
	
	private $iUserID;
	
	private $objLog;

	function PromotionImpl($strLanguage, $userid)
	{
		parent::Common();
		
		$this->intLinkSign = $this->getConnection($strLanguage);
		
		$this->objLog 	= new Logs($userid);
		$this->iUserID 	= $userid;
	}

	/* get data via ID
	 *
	 * @param  int    id
	 * @return mixed  false:
	 */
	public function getById($arrPopedom = array(), $id)
	{
		if($this->isUnallowableView($arrPopedom))
			die($this->getNotPopedomInfo());
		
		$sql = "SELECT * FROM promotion WHERE id = $id";
		$rs  = mysql_query($sql, $this->intLinkSign);
		if($rs)
			return mysql_fetch_array($rs);
		else
			return false;
	}

	/* 
	 *
	 * @param  string 	userid		
	 * @param  string 	number		man number  
	 * @param  string  	position    position
	 * @param  string 	summary		summary 
	 * @param  string 	descript	descript  
	 * @param  datetime	startDate	start date
	 * @param  datetime endDate		end date
 	 * @return int    	true: insert ID
	 */
    public function add($arrPopedom = array(), $productId, $descript, $status = 'normal')
	{
		if($this->isUnallowableAdd($arrPopedom))
			die($this->getNotPopedomInfo());
		$this->objLog->addOperate($this->iUserID, __METHOD__, Popedom::STRING_ADD);
		
		
		$sql = "INSERT INTO promotion SET productId='$productId', descript = '$descript', status = '$status'";
		
		$rs  = mysql_query($sql, $this->intLinkSign) or die (mysql_error($this->intLinkSign));
		if($rs)
			return true;
		else
			return false;
	}

	/* edit
	 *
	 * @param  string 	userid		news class name 
	 * @param  string 	number		man number  
	 * @param  string  	position    position
	 * @param  string 	summary		summary 
	 * @param  string 	descript	descript  
	 * @param  datetime	startDate	start date
	 * @param  datetime endDate		end date
 	 	 * @param  return 	boolean
	 */
	public function edit($arrPopedom = array(), $id, $productId, $descript)
	{
		if($this->isUnallowableEdit($arrPopedom))
			die($this->getNotPopedomInfo());
		$this->objLog->addOperate($this->iUserID, __METHOD__, Popedom::STRING_VIEW);
		
		$sql = "UPDATE promotion SET productId='$productId', descript = '$descript' WHERE id = $id";
		$rs	 = mysql_query($sql, $this->intLinkSign);
		if($rs)
			return true;
		else
			return false;
	}
	
	/* delete news class
	 *
	 * @param  int    	id
	 * @param  string  	status
	 * @param  return 	boolean
	 */
	public function delete($arrPopedom = array(), $id)
	{
		if($this->isUnallowableDelete($arrPopedom))
			die($this->getNotPopedomInfo());
		$this->objLog->addOperate($this->iUserID, __METHOD__, Popedom::STRING_DELETE, "id=$id");
			
		$sql = "DELETE FROM promotion WHERE id = $id";
		
		$rs	 = mysql_query($sql, $this->intLinkSign) or die ($sql . mysql_error($this->intLinkSign));
		if($rs)
			return true;
		else
			return false;
	}
	
	/* list
	 *
	 * @ param  string 	class id
	 * @ param  string 	page
	 * @ param  string 	discount
	 * @ param  string 	url
	 * @ param  return 	array
	 */
	public function lists($arrPopedom = array(), $languageID, $where, $page = '', $discount = '', $url = '')
	{
		if($this->isUnallowableView($arrPopedom))
			die($this->getNotPopedomInfo());
		$this->objLog->addOperate($this->iUserID, __METHOD__, Popedom::STRING_VIEW);
			
		$arrback = array();

		$sql = "SELECT count(*) FROM promotion s, products_description pd, products p WHERE p.id = pd.productID AND s.productID = pd.productID AND languageID=$languageID $where";

		$rs  = mysql_query($sql, $this->intLinkSign) or die (mysql_error($this->intLinkSign));
		list($total) = mysql_fetch_row($rs);
		$this->mutilPager($total, $page, $discount, $url);
		if(empty($total))
 			return $arrback;

		if(empty($discount))
			$sql = "SELECT s.id, s.descript, pd.name, pd.description, s.productId, p.price, s.status FROM promotion s, products_description pd, products p WHERE p.id = pd.productID AND s.productId = pd.productID AND languageID=$languageID $where";
		else{
			$sql = "SELECT s.id, s.descript, pd.name, pd.description, s.productId, p.price, s.status FROM promotion s, products_description pd, products p WHERE p.id = pd.productID AND s.productId = pd.productID AND languageID=$languageID $where LIMIT $this->firstcount, $discount";
		}
		$rs  = mysql_query($sql, $this->intLinkSign);
		if($rs){
			while(is_array($arrtmp = mysql_fetch_array($rs))){
				$arrback[] = $arrtmp;
			}
			return $arrback;
		}else
 			return $arrback;
	}
	
	public function IsExistName($languageID, $name)
	{
		$sql = "SELECT id FROM promotion WHERE languageID = $languageID AND name = '$name'";
		$rs	 = mysql_query($sql, $this->intLinkSign) or die ("error: " . mysql_error());
		if(mysql_num_rows($rs) > 0)
			return true;
		else
			return false;
	}
	
	/* list by condition
	 *
	 * @param  string condition
	 * @return mixed  false: sql error  array: news info
	 */
	public function listByCustom($arrPopedom = array(), $condition='')
	{
		if($this->isUnallowableView($arrPopedom))
			die($this->getNotPopedomInfo());
		$this->objLog->addOperate($this->iUserID, __METHOD__, Popedom::STRING_VIEW);
			
		$arrBack = array();
		$sql = "SELECT * FROM promotion".$condition;
        $rs  = mysql_query($sql, $this->intLinkSign);
		if($rs){
			while($arrCache = mysql_fetch_array($rs)){
				$arrBack[] = $arrCache;
			}
			return $arrBack;
		}else
			return false;
	}

	/* edit user status
	 *
 	 * @ param  int    userid
	 * @ param  string status	
	 * @ param  return boolean
	 */
	public function setStatus($arrPopedom, $id, $status)
	{
		if($this->isUnallowable($arrPopedom, Popedom::STRING_EDIT))
			die ($this->getNotPopedomInfo());
		
		$this->objLog->addOperate($this->iUserID, __METHOD__, Popedom::STRING_EDIT, "status=$status, id=$id");
			
		$sql = "UPDATE promotion SET status='$status' WHERE id=$id";
		$rs	 = mysql_query($sql,$this->intLinkSign);
		if(mysql_affected_rows() > 0)
			return true;
		else
			return false;
	}

}
?>
